package com.ziq.zcamp.util

import android.os.Environment
import com.ziq.zcamp.manager.GlobalContextManager
import java.io.File
import java.util.*

class StorageUtil {
    companion object {
        val logCachePath: String = initLogCachePath()
        val recorderLogPath: String = initRecorderLogPath()
        val compressPath: String = initCompressPicturePath()
        val cropPath: String = initCropPath()
        val videoPath: String = initVideoPath()
        const val KB = 1024.0
        const val MB = KB * KB
        const val GB = KB * KB * KB

        init {
            createAllFolder()
        }

        private fun createAllFolder() {
            delete(cropPath)

            createDir(logCachePath)
            createDir(recorderLogPath)
            createDir(compressPath)
            createDir(cropPath)
            createDir(videoPath)
        }

        /**
         * 获得缓存文件大小
         */
        fun getCacheSize(): String? {
            var cacheSize: Long = 0
            cacheSize += getFileSize(File(compressPath))
            cacheSize += getFileSize(File(cropPath))
            cacheSize += getFileSize(File(videoPath))
            return fileSizeStr(cacheSize)
        }

        /**
         * 清空缓存文件夹
         */
        fun clearCache() {
            delete(compressPath)
            delete(cropPath)
            delete(videoPath)
            createAllFolder()
        }

        fun clearVideoCache() {
            delete(videoPath)
            createDir(videoPath)
        }

        /**
         * 上传日志
         */
        fun getLogPaths(): ArrayList<String> {
            val logPaths = ArrayList<String>()
            logPaths.add(recorderLogPath) // IKLog存放路径
            logPaths.add("/data/anr/traces.txt") // anr崩溃日志存放路径
            return logPaths
        }



        private fun initLogCachePath(): String {
            val context = GlobalContextManager.appContext
            return context?.filesDir?.absolutePath + File.separator + "logCache"
        }

        private fun initRecorderLogPath(): String {
            val context = GlobalContextManager.appContext
            return context?.filesDir?.absolutePath + File.separator + "xlog"
        }

        private fun initCompressPicturePath(): String {
            val context = GlobalContextManager.appContext
            return context?.filesDir?.absolutePath + File.separator + "crop"
        }

        private fun initCropPath(): String {
            val context = GlobalContextManager.appContext
            return context?.getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.absolutePath + ""
        }

        private fun initVideoPath(): String {
            val context = GlobalContextManager.appContext
            return context?.filesDir?.absolutePath + File.separator + "video"
        }

        fun getPictureFileWith(name: String, del: Boolean = true): File {
            val cache = File(cropPath, name)
            if (del && cache.exists()) {
                cache.delete()
            }
            return cache
        }

        fun getVideoFileWith(name: String, del: Boolean = true): File {
            val cache = File(videoPath, name)
            if (del && cache.exists()) {
                cache.delete()
            }
            return cache
        }


        private fun createDir(path: String) {
            try {
                val file = File(path)
                if (!file.exists()) {
                    file.mkdirs()
                }
            }catch (e:Exception){
                e.printStackTrace()
            }
        }

        /**
         * 获取文件大小转换成Str[k,M,G]
         */
        fun fileSizeStr(size: Long): String? {
            var fileSize = ""
            if (size < KB) {
                fileSize = "${size}B"
            } else if (size < MB) {
                fileSize = String.format(Locale.UK, "%.1f", size / KB) + "K"
            } else if (size < GB) {
                fileSize = String.format(Locale.UK, "%.1f", size / MB) + "M"
            } else {
                fileSize = String.format(Locale.UK, "%.1f", size / GB) + "G"
            }
            return fileSize
        }

        /**
         * 获取文件夹大小 前提是此文件夹下不能有文件夹
         */
        fun getFileSize(dir: File?): Long {
            var dirSize: Long = 0
            if (null == dir || !dir.isDirectory) {
                return dirSize
            }
            val files = dir.listFiles()
            if (null == files || files.size <= 0) {
                return dirSize
            }
            for (file in files) {
                dirSize += file.length()
            }
            return dirSize
        }


        fun clearDir(path: String){
            if(path.isEmpty()){
                return
            }
            val dir = File(path)
            if (dir.exists() && dir.isDirectory) {
                val files = dir.listFiles()
                for (item in files){
                    delete(item)
                }
            }
        }

        fun delete(path: String?): Boolean {
            return if (path == null || path.trim().isEmpty()) {
                false
            } else try {
                delete(File(path))
            } catch (e: Exception) {
                false
            }
        }

        fun delete(file: File?): Boolean {
            if (null == file) {
                return false
            }
            if (!file.exists()) {
                return false
            }
            if (file.isFile) {
                return file.delete()
            }
            if (!file.isDirectory) {
                return false
            }
            for (f in file.listFiles()) {
                if (f.isFile) {
                    f.delete()
                } else if (f.isDirectory) {
                    delete(f.absolutePath)
                }
            }
            return file.delete()
        }

    }
}